home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
bc
/
pro7
/
fcopy.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-11-25
|
6KB
|
142 lines
/*--------------------------------------------------------------------------
* filecopy.c
*
* Original Code by Ed Mulroy 1992
* Modified by Al Gifford 1993
*
* Original code was written to demonstrate a file copy from inside a C
* program. This code is a function which can be easily included into your
* code to allow you to copy files from one directory to another.
*
* If you get errors indicating there is not enough memory, you can either
* decrease the size of the copy buffer, or you can increase the size of
* your memory model.
*
* There are still many changes which could be made to this function. Feel
* free to do so.
*
* Version 1.10 - added some features that users requested
* - error checking on reads & writes
* - setting DOS' verify flag on
* - reporting DOS error messages
* - writes are of the number actually read
* rather than the number requested to be read
*
* Version 1.20 - Changed copy program example to a function call.
* - free'd copy buffer after function call.
* - modified new file's time and date stamp to accurately
* reflect the time and date stamp of the original file.
*
* permission granted for any private or commercial use
*
*/
#include <stdio.h> /* fputs(), putchar(), perror(), _fmode */
#include <stdlib.h> /* malloc(), free(), exit() */
#include <sys\stat.h> /* S_IWRITE */
#include <fcntl.h> /* O_BINARY */
#include <dos.h> /* setverify(), getverify() */
#include <io.h> /* open(), creat(), read(), write(),
close(), access(), filelength() */
#define BUF_SIZE (32 * 1024U) /* copy buffer size, DOS limit is < 64K */
int save_verify; /* save the old value of the verify flag */
char *logo = "Filecopy v1.20\n";
char *read_error_msge = "Read file error";
char *write_error_msge = "Write file error";
void fatal(char *msge) /* give an error message and quit */
{
fputs(msge,stderr); /* write to stderr so it can't be re-directed */
setverify(save_verify); /* restore old verify flag */
exit(2); /* return 2 as some systems return 1 for success */
}
void file_error(char *msge) /* give our & DOS error message and quit */
{
perror(msge); /* give our and DOS' error messages */
setverify(save_verify); /* restore old verify flag */
exit(2);
}
int fcopy(char *file1,char *file2)
{
long numleft; /* number of bytes remaining to copy */
unsigned numnow; /* number of bytes for this pass */
unsigned numxfer; /* number of bytes actually read or written */
int inhand; /* input file handle */
int outhand; /* output file handle */
char *buffer; /* pointer which will be copy buffer address */
char ch;
struct ftime ft;
fputs(logo,stderr); /* show program name */
save_verify = getverify();
setverify(1); /* set DOS' verify file flag */
if((buffer = (char *)malloc(BUF_SIZE)) == NULL) /* allocate copy buffer */
fatal("Not enough memory to run program\n");
if((inhand = open(file1,O_RDONLY|O_BINARY)) == -1)
file_error("Error opening source file");
if(!access(file2,0)) /* test for destination file existence */
{
fputs( "Destination file exists. Overwrite? [Y/N] ", stderr);
fflush(stdin);
ch = getchar();
if((ch != 'y') && (ch != 'Y'))
fatal("\nCopying aborted\n");
}
_fmode = O_BINARY; /* creat() uses _fmode for file mode */
if((outhand = creat(file2, S_IWRITE)) == -1)
file_error("Error creating destination file");
numleft = filelength(inhand); /* get source file size */
fputs("copying ",stdout);
while(numleft > 0)
{
putchar('.'); /* show activity */
if(numleft > (long) BUF_SIZE) /* decide on this pass copy size */
numnow = BUF_SIZE;
else
numnow = (unsigned) numleft;
numxfer = read(inhand, buffer, numnow);
if(numxfer == (unsigned) -1) /* if a DOS error on reading */
file_error(read_error_msge);
else if(numxfer == 0) /* if 0, DOS doesn't report an error */
fatal(read_error_msge);
numxfer = write(outhand,buffer,numxfer);
if(numxfer == (unsigned) -1) /* if a DOS error on writing */
file_error(write_error_msge);
else if(numxfer == 0) /* if 0, DOS doesn't report an error */
fatal(write_error_msge);
numleft -= numxfer; /* decrement amount left to copy */
}
getftime(inhand,&ft); /* get time and date */
setftime(outhand,&ft); /* set time and date */
if(close(inhand) == -1)
file_error("Input file close error");
if(close(outhand) == -1)
file_error("Output file close error");
free(buffer);
puts(" Done");
return 0;
}